package com.base;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;

/**
 * 父类抽象组件
 * 
 * @author 625
 *
 */
public abstract class Com<T> {

	private ActiveXComponent axc;

	protected Dispatch com;

	/**
	 * 0表示大漠，1表示键鼠
	 */
	public static int type = 0;
	/**
	 * 初始化组件
	 */
	static {
		ComThread.InitSTA();
	}

	public Com() {
		String setComName = setComName();
		try {
			init(setComName);
		} catch (Exception e) {
			Base.sleeps(1);
			System.out.println("first init "+setComName+" fail,try again!");
			Base.sleeps(1);
			reg();
			Base.sleeps(1);
			init(setComName);
			Base.sleeps(1);
		}
	
	}

	/**
	 * 初始化com
	 */
	private void init(String setComName) {
		axc = new ActiveXComponent(setComName);
		com = (Dispatch) axc.getObject();
		System.out.println(setComName + " init success！");
	}

	/**
	 * 调用
	 * 
	 * @param method 方法名
	 * @param params 参数
	 * @return
	 */
	protected abstract String call(T method, Object... params);

	/**
	 * 注册com
	 * 
	 * @return
	 */
	protected abstract void reg();

	/**
	 * 设置注册com的名字
	 * 
	 * @return
	 */
	protected abstract String setComName();

	/**
	 * 关闭com组件线程
	 */
	public static void close() {
		ComThread.Release();
	}

}
